package com.szholly.utils.session.provider;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.szholly.utils.session.CurrentSessionHelper;
import org.springframework.stereotype.Component;

/**
 * 定基Http的Session提供对象
 */
@Component("sessionProvider")
public class HttpSessionProvider implements ISessionProvider {

	public Serializable getAttribute(HttpServletRequest request, String name) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			return (Serializable) session.getAttribute(name);
		} else {
			return null;
		}
	}

	public void setAttribute(HttpServletRequest request,
			HttpServletResponse response, String name, Serializable value) {
		HttpSession session = request.getSession();
		session.setAttribute(name, value);
	}

	public String getSessionId(HttpServletRequest request,
			HttpServletResponse response) {
		return request.getSession().getId();
	}

	public void logout(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			session.invalidate();
		}
	}

	/**
	 * 获取IP
	 * 
	 * @return
	 */
	public String getIp() {
		return getIpAddr(CurrentSessionHelper.getCurrentHttpServletRequest());
	}

	public static String getIpAddr(HttpServletRequest request) {
		if(request==null){
			return "";
		}
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 获取当前登录的用户名
	 * 
	 * @return
	 */
	@Override
	public String getUserName() {
		return (String) this.getObject(UserName);
	}

	@Override
	public String getUserName(HttpSession sesison) {
		return (String) this.getObject(sesison, UserName);
	}

	/**
	 * 设置当前登录的用户名
	 * 
	 * @param userName
	 */
	@Override
	public void setUserName(String userName) {
		this.setObject(UserName, userName);
	}

	@Override
	public void setUserName(HttpSession sesison, String userName) {
		this.setObject(sesison, UserName, userName);
	}

	/**
	 * 获取当前登录的密码
	 * 
	 * @return
	 */
	@Override
	public String getPassword() {
		return (String) this.getObject(Password);
	}

	@Override
	public String getPassword(HttpSession sesison) {
		return (String) this.getObject(sesison, Password);
	}

	/**
	 * 设置当前登录的密码
	 * 
	 * @param password
	 */
	@Override
	public void setPassword(String password) {
		this.setObject(Password, password);
	}

	@Override
	public void setPassword(HttpSession sesison, String password) {
		this.setObject(sesison, Password, password);
	}

	/**
	 * 获取当前的系统名称
	 * 
	 * @return
	 */
	@Override
	public String getOrgName() {
		return (String) this.getObject(OrgName);
	}

	@Override
	public String getOrgName(HttpSession sesison) {
		return (String) this.getObject(sesison, OrgName);
	}

	/**
	 * 设置当前的系统名称
	 * 
	 * @param systemName
	 */
	@Override
	public void setOrgName(String systemName) {
		this.setObject(OrgName, systemName);
	}

	@Override
	public void setOrgName(HttpSession sesison, String orgName) {
		this.setObject(sesison, OrgName, orgName);
	}

	@Override
	public Object getObject(String key) {
		HttpSession session = CurrentSessionHelper.getCurrentSession();
		return getObject(session, key);
	}

	@Override
	public Object getObject(HttpSession session, String key) {
		if (session == null) {
			return null;
		}
		return session.getAttribute(key);
	}

	@Override
	public void setObject(String key, Object obj) {
		HttpSession session = CurrentSessionHelper.getCurrentSession();
		setObject(session, key, obj);
	}

	@Override
	public void setObject(HttpSession session, String key, Object obj) {
		if (session == null) {
			return;
		}
		session.setAttribute(key, obj);
	}

	@Override
	public void remove(String key) {
		HttpSession session = CurrentSessionHelper.getCurrentSession();
		remove(session, key);
	}

	@Override
	public void remove(HttpSession session, String key) {
		if (session == null) {
			return;
		}
		session.removeAttribute(key);
	}

	@Override
	public void clear() {
		HttpSession session = CurrentSessionHelper.getCurrentSession();
		clear(session);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void clear(HttpSession session) {
		if (session == null) {
			return;
		}
		Enumeration items = session.getAttributeNames();
		List itemList = new ArrayList();
		while (items.hasMoreElements()) {
			Object item = items.nextElement();
			itemList.add(item);
		}
		for (Object i : itemList) {
			session.removeAttribute(i.toString());
		}
	}
}
